package b3.widget.home.social
{
	import b3.model.*;
	import b3.net.*;
	import b3.widget.*;
	import b3.widget.basic.*;
	import b3.widget.mail.*;
	import b3.widget.warehouse.*;
	
	import flash.display.*;
	import flash.events.*;

	public class SocialPane extends BasicWidget
	{
		private var mailIcon:MailReceiveIcon;
		private var socialIcon:SocialIcon;
		
		public function SocialPane()
		{
			super();
			this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			var me:Player = PlayerManager.getInstance().me;
			me.addEventListener(ModelEvent.LOAD_MAIL_COMPLETE, onLoadMailComplete);
			me.addEventListener(ModelEvent.GET_SOCIAL_INFO, onGetSocialInfoComplete);
			me.addEventListener(ModelEvent.SET_SOCIAL_MESSAGE_VIEWED, onSetSocialMessageViewed);
			mailIcon = new MailReceiveIcon();
			socialIcon = new SocialIcon(); 
			this.anchor = BasicWidget.ANCHOR_LEFT_BOTTOM;
			this.x = 20;
			this.y = -150;
		}
		
		private function onAddedToStage(event:Event):void
		{
			var me:Player = PlayerManager.getInstance().me; 
			me.getSocialInfo(true);
			me.loadMail(true);
		}
		
		private function onSetSocialMessageViewed(event:ModelEvent):void
		{
			socialIcon.stopShine();
		}
		
		private function onGetSocialInfoComplete(event:ModelEvent):void
		{
			var me:Player = PlayerManager.getInstance().me;
			if (me.socialInfos != null && me.socialInfos.length > 0)
			{
				showIcon(socialIcon);
				if (me.hasNewSocialInfoFlag)
					socialIcon.startShine();
			}
			else
			{
				hideIcon(socialIcon);
				socialIcon.stopShine();
			}
		}
		
		private function onLoadMailComplete(event:ModelEvent):void
		{
			var me:Player = PlayerManager.getInstance().me;
			if (me.mails != null && me.mails.length > 0)
			{
				showIcon(mailIcon);
				mailIcon.startShine();
			}
			else
			{
				hideIcon(mailIcon);
				mailIcon.stopShine();
			}
		}
		
		private function showIcon(icon:Sprite):void
		{
			if (icon.parent == null)
				addChild(icon);
			refreshIconPos();
		}
		
		private function hideIcon(icon:Sprite):void
		{
			if (icon.parent != null)
				icon.parent.removeChild(icon);
			refreshIconPos();
		}
		
		private function refreshIconPos():void
		{
			for (var i:int = 0; i < this.numChildren; i++)
			{
				var icon:DisplayObject = this.getChildAt(i);
				icon.x = 100 * i;
			} 
		}
		
	}
}